<template>
  <div class="app-container">
    <h4>积分统计</h4>
    <div class="total-layout">
      <el-row :gutter="20" v-for="(item,index) in totalintegr" :key="index">
        <el-col :span="4">
          <div class="grid-content bg-purple">
            <p class="title">总发放积分</p>
            <p class="price">{{item.totalPoints}}</p>
            <p class="other">昨日发放 ￥{{item.yesterdayPoints}}</p>
          </div>
        </el-col>
        <el-col :span="4">
          <div class="grid-content bg-purple">
            <p class="title">新增会员积分</p>
            <p class="price">{{item.registerPoints}}</p>
            <p class="other">&nbsp;</p>
          </div>
        </el-col>
        <el-col :span="4">
          <div class="grid-content bg-purple">
            <p class="title">购物奖励积分</p>
            <p class="price">{{item.consumePoints}}</p>
            <p class="other">&nbsp;</p>
          </div>
        </el-col>
        <el-col :span="4">
          <div class="grid-content bg-purple">
            <p class="title">已兑换积分</p>
            <p class="price">{{item.extractedPoints}}</p>
            <p class="other">&nbsp;</p>
          </div>
        </el-col>
      </el-row>
    </div>
    <div class="table-container">
      <el-table ref="couponTable" :data="list" style="width: 100%;" @selection-change="handleSelectionChange" v-loading="listLoading" border="">
        <!-- <el-table-column type="index" label="序号" width="100" align="center"></el-table-column> -->
        <el-table-column label="积分类型" align="center">
          <template slot-scope="scope">{{scope.row.type| formatType}}</template>
        </el-table-column>
        <el-table-column label="获积分数" align="center">
          <template slot-scope="scope">{{scope.row.points }}</template>
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">{{scope.row.status | formatStatus}}</template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handleUpdate(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="text" @click="handleDelete(scope.$index, scope.row)" v-if="scope.row.status==1">停用</el-button>
            <el-button size="mini" type="text" @click="handleDelete(scope.$index, scope.row)" v-if="scope.row.status==0">启用</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 类型: 0推荐积分,1消费积分,2留言积分,3反馈积分 -->
    <el-dialog :title="this.title" :visible.sync="recommendedpoints" width="30%">
      <el-form :model="returnReason" ref="reasonForm" label-width="80px">
        <el-form-item label="积分设置">
          <el-input v-model="returnReason.points" class="input-width"></el-input>
          <div class="tips">{{this.tips}}</div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="recommendedpoints = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirm">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  fetchList,
  Memberstatistics,
  deleteCoupon,
  addReason,
  updateReason,
  getReasonDetail
} from "@/api/integralRule";
const defaultListQuery = {
  pageNum: 1,
  pageSize: 10,
  merchantId: 1
  // name: null,
  // type: null
};
const defaultReturnReason = {
  points: null,
  id: null
};
export default {
  name: "couponList",
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      returnReason: Object.assign({}, defaultReturnReason),
      list: null,
      total: null,
      title: null,
      tips: null,
      totalintegr: null,
      listLoading: false,
      multipleSelection: [],
      // img_home_order,
      // img_home_today_amount,
      // img_home_yesterday_amount,
      messagescore: false,
      recommendedpoints: false,
      consumptionpoints: false,
      feedbackcredit: false,
      operateReasonId: null
    };
  },
  created() {
    this.getData();
    this.getList();
  },
  filters: {
    // 类型: 0推荐积分,1消费积分,2留言积分,3反馈积分
    formatType(type) {
      if (type == 0) {
        return "推荐积分";
      } else if (type == 1) {
        return "消费积分";
      } else if (type == 2) {
        return "留言积分";
      } else if (type == 3) {
        return "反馈积分";
      }
    },
    // formatUseType(useType) {
    //   if (useType === 0) {
    //     return "全场通用";
    //   } else if (useType === 1) {
    //     return "指定分类";
    //   }
    // },
    formatPlatform(platform) {
      if (platform === 1) {
        return "移动平台";
      } else if (platform === 2) {
        return "PC平台";
      } else {
        return "全平台";
      }
    },
    formatDate(time) {
      if (time == null || time === "") {
        return "N/A";
      }
      let date = new Date(time);
      return formatDate(date, "yyyy-MM-dd");
    },
    // 状态: 0已停用,1已启用
    formatStatus(status) {
      let now = new Date().getTime();
      if (status == 0) {
        return "已停用";
      } else if (status == 1) {
        return "已启用";
      }
    }
  },
  methods: {
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
    },
    handleSearchList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    handleAdd() {
      this.$router.push({ path: "/sms/addCoupon" });
    },
    handleView(index, row) {
      this.$router.push({ path: "/sms/couponHistory", query: { id: row.id } });
    },
    // 类型: 0推荐积分,1消费积分,2留言积分,3反馈积分

    handleUpdate(index, row) {
      this.recommendedpoints = true;
      this.operateReasonId = row.id;
      if (row.type == 0) {
        this.title = "推荐积分";
        this.tips = "每成功推荐一人所获积分";
      } else if (row.type == 1) {
        this.title = "消费积分";
        this.tips = "消费1000所获积分";
      } else if (row.type == 2) {
        this.title = "留言积分";
        this.tips = "一条入选留言所获积分";
      } else if (row.type == 3) {
        this.title = "反馈积分";
        this.tips = "一个获奖用户所获积分";
      }
      getReasonDetail(row.id).then(response => {
        this.returnReason = response.data;
      });
    },
    handleDelete(index, row) {
      let params = new URLSearchParams();
      params.append("id", row.id);
      if (row.status == 0) {
        params.append("status", 1);
      } else {
        params.append("status", 0);
      }
      this.$confirm("确认停用该活动?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        deleteCoupon(params).then(response => {
          this.$message({
            type: "success",
            message: "成功!"
          });
          this.getList();
        });
      });
    },
    getData() {
      Memberstatistics(1).then(response => {
        this.totalintegr = response.data;
      });
    },
    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then(response => {
        this.listLoading = false;
        this.list = response.data.list;
        this.total = response.data.total;
      });
    },
    // 推荐积分
    handleConfirm() {
      if (this.operateReasonId == null) {
      } else {
        let params = new URLSearchParams();
        params.append("id", this.operateReasonId);
        params.append("points", this.returnReason.points);
        //编辑操作
        updateReason(params).then(response => {
          this.recommendedpoints = false;
          this.consumptionpoints = false;
          this.messagescore = false;
          this.feedbackcredit = false;
          this.operateReasonId = null;
          this.$message({
            message: "修改成功！",
            type: "success",
            duration: 1000
          });
          this.getList();
        });
      }
    },
    // 消费积分
    handlepointsConfirm() {
      if (this.operateReasonId == null) {
        //添加操作
        // addReason(this.returnReason).then(response => {
        //   this.dialogVisible = false;
        //   this.operateReasonId = null;
        //   this.$message({
        //     message: "添加成功！",
        //     type: "success",
        //     duration: 1000
        //   });
        //   this.getList();
        // });
      } else {
        //编辑操作
        let params = new URLSearchParams();
        params.append("id", this.operateReasonId);
        params.append("status", this.returnReason.status);
        updateReason(params).then(response => {
          this.consumptionpoints = false;
          this.operateReasonId = null;
          this.$message({
            message: "修改成功！",
            type: "success",
            duration: 1000
          });
          this.getList();
        });
      }
    },
    // 留言积分
    handlemessaConfirm() {
      if (this.operateReasonId == null) {
        //添加操作
        // addReason(this.returnReason).then(response => {
        //   this.dialogVisible = false;
        //   this.operateReasonId = null;
        //   this.$message({
        //     message: "添加成功！",
        //     type: "success",
        //     duration: 1000
        //   });
        //   this.getList();
        // });
      } else {
        //编辑操作
        let params = new URLSearchParams();
        params.append("id", this.operateReasonId);
        params.append("status", this.returnReason.status);
        updateReason(params).then(response => {
          this.messagescore = false;
          this.operateReasonId = null;
          this.$message({
            message: "修改成功！",
            type: "success",
            duration: 1000
          });
          this.getList();
        });
      }
    },
    // 反馈积分
    handlefeedConfirm() {
      if (this.operateReasonId == null) {
        //添加操作
        // addReason(this.returnReason).then(response => {
        //   this.dialogVisible = false;
        //   this.operateReasonId = null;
        //   this.$message({
        //     message: "添加成功！",
        //     type: "success",
        //     duration: 1000
        //   });
        //   this.getList();
        // });
      } else {
        //编辑操作
        let params = new URLSearchParams();
        params.append("id", this.operateReasonId);
        params.append("status", this.returnReason.status);
        updateReason(params).then(response => {
          this.feedbackcredit = false;
          this.operateReasonId = null;
          this.$message({
            message: "修改成功！",
            type: "success",
            duration: 1000
          });
          this.getList();
        });
      }
    }
  }
};
</script>
<style scoped >
.app-container {
  background: #fff;
}
.title {
  font-size: 14px;
}
.el-row {
  display: flex;
  margin-bottom: 20px;
}
.el-col {
  border: 1px solid #ccc;
  padding: 0 10px;
  box-sizing: border-box;
  border-radius: 4px;
  margin-right: 15px;
}
.price {
  font-size: 16px;
  font-weight: 600;
  border-bottom: 1px solid #ccc;
  line-height: 32px;
  box-sizing: border-box;
}
.other {
  font-size: 14px;
  color: #7d7c7c;
}
.input-width {
  width: 203px;
}

.total-layout {
  margin-top: 20px;
}

.total-frame {
  border: 1px solid #dcdfe6;
  padding: 20px;
  height: 180px;
}

.total-icon {
  color: #409eff;
  width: 60px;
  height: 60px;
}

.total-title {
  position: relative;
  font-size: 16px;
  color: #909399;
  left: 70px;
  top: -50px;
}

.total-value {
  position: relative;
  font-size: 18px;
  color: #606266;
  left: 70px;
  top: -40px;
}
</style>


